package com.marketingexpert.controller.marketingmgnt.activityrecomment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class ListRecMealController implements Controller {

	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse arg1) throws Exception {
		
		request.setCharacterEncoding("utf8");
		
		String dispatchUrl = request.getParameter("dispatchUrl");
		String[] mealIds = request.getParameterValues("mealId");
		
		String mealNames = request.getParameter("mealNames");
		
		String[] mealNamesArr = new String[mealIds.length];
		
		if(mealIds.length > 1) {
			mealNamesArr = mealNames.split(",");
		} else {
			mealNamesArr[0] = mealNames;
		}
		
		List<Map> _res = new ArrayList<Map>();
		
		for(int i=0; i<mealIds.length; i++) {
			Map<String, String> results = new HashMap<String, String>();
			results.put("mealId", mealIds[i]);
			results.put("title", mealNamesArr[i]);
			request.getSession().removeAttribute("rec_" + mealIds[i]);
			_res.add(results);
		}
		
		return new ModelAndView(dispatchUrl,"results",_res);
	}

}
